import {createRouter, createWebHistory} from 'vue-router'
import Layout from '../layout/Layout'
import store from '../store'

const routes = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/Login.vue')
    },



    {
        path: '/',
        component: Layout,
        redirect: '/home',
        children:[
            {
                name: 'home',
                path: '/home',
                meta: { title: '首页'},
                component:() => import("@/views/Home")

            },
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: { title: '控制台'},
                component:() => import("@/views/Dashboard")
            },
        ]
    },


    {
        path: "/sys",
        name: 'sys',
        redirect: '/user',
        meta: {title: '系统管理'},
        component: Layout,
        children: [
            {
                path: '/user',
                name: 'user',
                meta: {title: '用户管理'},
                component: () => import("@/views/sys/User"),

            },
            {
                path: '/role',
                name: 'role',
                meta: {title: '角色管理',},
                component: () => import("@/views/sys/Role"),
            },
            {
                path: '/menu',
                name: 'menu',
                meta: {title: '菜单管理',},
                component: () => import("@/views/sys/Menu"),
            },

            {
                path: '/dict',
                name: 'dict',
                meta: {title: '字典管理'},
                component: () => import("@/views/sys/Dict"),
            },

            {
                path: '/data/:id',
                name: 'dictData',
                meta: {title: '字典数据'},
                component: () => import("@/views/sys/DictData"),
            },

            {
                path: '/authrole/:userId',
                name: 'authrole',
                meta: {title: '分配角色'},
                component: () => import("@/views/sys/authrole")
            },

            {
                path: '/authuser/:roleId',
                name: 'authuser',
                meta: {title: '分配用户'},
                component: () => import("@/views/sys/authuser")
            },

            {
                path: 'https://www.baidu.com',
                name: 'baidu',
                meta: {title: '官网'},
            },



        ]

    },

]




const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})


//挂载路由导航守卫 to:将要访问的路径  from:代表从哪个路径跳转而来  next是一个函数，表示放行
router.beforeEach((to, from, next) => {
    //如果用户访问的是登录页，直接放行
    store.commit('getToken');  //这里调用store里面的token,是为了防止刷新之后，vuex丢失我们的token信息
    const token = store.state.user.token;   //获取store中的token
    //如果token不存在并且当前登录页不是login   则强制跳转到登录页
    if(!token && to.name !== 'login') {
        sessionStorage.clear();
        return next({name:'login'});
    }else{
        next()
    }

})

export default router
